﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace V5_EShop
{
    public partial class OrderModAction : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var user = (V5_Model.ModelGZUser)Session["isLogin"];
            var type = Request["type"] + string.Empty;
            string guid = Request["id"] + string.Empty;
            int amount = 1;

            if(!int.TryParse((Request["amount"]+string.Empty).Trim(),out amount))
                amount=1;


            if (user == null)
            {
                WriteMsg("请先登录！");
            }


            switch (type)
            {
                case "0":
                    DelParentOrder(guid);
                    break;
                case "1":
                    DelChildOrder(guid);
                    break;
                case "2":
                    UptItem(guid, amount);
                    break;
                default:
                    WriteMsg("-100");
                    break;
            } 

            WriteMsg("0"); 
        }

        public string DelParentOrder(string guid)
        {
            DataTable dt2 = new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().GetList(" ParentID='" + guid + "'").Tables[0];
           
            if (dt2 != null && dt2.Rows.Count > 0)
            {
                foreach (DataRow dr in dt2.Rows)
                {
                    new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().Delete(Guid.Parse(dr["ID"] + string.Empty));
                }
            }
            new V5_EShop._Class.BLL.GZ.BLLGZProductOrder().Delete(Guid.Parse(guid));

            return "0";
        }


        public string DelChildOrder(string guid)
        {
            new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().Delete(Guid.Parse(guid));
            return "0";
        }

        public string UptItem(string guid, int amount)
        {
            var item = new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().GetModel(Guid.Parse(guid));

            if (item != null)
            {
                item.OrderNumber = amount;
                item.OrderMoney = item.OrderPrice * decimal.Parse(amount.ToString());

                new V5_EShop._Class.BLL.GZ.BLLGZProductOrderItem().Update(item);
            }

            return "0";

        }



        private void WriteMsg(string info)
        {
            Response.Write(info);
            Response.End();
        }
    }
}